package com.rchat.pocmini.activity.fragment;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.airtalkee.sdk.entity.AirSession;
import com.rchat.pocmini.R;
import com.rchat.pocmini.activity.bean.GroupBean;

import java.util.List;

/**
 * Created by itsdf07 on 2017/8/25.
 */

public class GroupsAdapter extends RecyclerView.Adapter<GroupsAdapter.GroupsHolder> {
    private List<GroupBean> mGroupsData;
    private ItemTouchHelperAdapterCallback mItemTouchHelperAdapterCallback;

    /**
     *
     */
    public interface ItemTouchHelperAdapterCallback {
        /**
         * 设置频道监听：sessionCall
         *
         * @param bean
         * @return
         */
        AirSession onItemChannelListened(GroupBean bean);

        /**
         * 设置频道锁
         *
         * @param bean
         */
        void onItemChannelLocked(GroupBean bean);

        /**
         * Item点击事件
         *
         * @param view
         * @param bean
         */
        void onItemClick(View view, GroupBean bean);

        /**
         * Item长按事件
         *
         * @param view
         * @param position
         */
        void onItemLongClick(View view, GroupBean bean, int position);


    }

    public GroupsAdapter(List<GroupBean> groupsData, ItemTouchHelperAdapterCallback callback) {
        this.mGroupsData = groupsData;
        this.mItemTouchHelperAdapterCallback = callback;
    }

    public void setData(List<GroupBean> groupsData) {
        mGroupsData = groupsData;
        notifyDataSetChanged();
    }

    @Override
    public GroupsAdapter.GroupsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.group_list_view_item, parent, false);
        return new GroupsHolder(view);
    }

    @Override
    public void onBindViewHolder(final GroupsHolder holder, final int position) {
        final GroupBean bean = mGroupsData.get(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemTouchHelperAdapterCallback != null) {
                    mItemTouchHelperAdapterCallback.onItemClick(v, bean);
                }
                notifyDataSetChanged();
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mItemTouchHelperAdapterCallback != null) {
                    mItemTouchHelperAdapterCallback.onItemLongClick(v, bean, position);
                }
                return true;
            }
        });
        holder.channelIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemTouchHelperAdapterCallback != null) {
                    mItemTouchHelperAdapterCallback.onItemChannelListened(bean);
                    setLockStatus(holder.channelLockIcon, bean.isLocked());
                    //TODO 刷新最好的性能是单个Ite刷新，但是这边单个Item刷新会出现闪一下，所以暂时先全部刷新
                    notifyDataSetChanged();
//                    notifyItemChanged(position);
                }
            }
        });
        holder.layoutLock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemTouchHelperAdapterCallback != null) {
                    mItemTouchHelperAdapterCallback.onItemChannelLocked(bean);
                }
                //TODO 刷新最好的性能是单个Ite刷新，但是这边单个Item刷新需记住上一个锁定频道，然后取消锁定，所以这边暂时先全部刷新
                notifyDataSetChanged();
//                notifyItemChanged(position);
            }
        });
        holder.groupName.setText(bean.getGroupName());
        holder.groupId.setText("ID:" + bean.getGroupId());

        if (bean.isShowTalking()) {
            holder.talking.setText(bean.getWhoIsTalking());
            holder.talking.setVisibility(View.VISIBLE);
        } else {
            holder.talking.setVisibility(View.GONE);
        }
        holder.channelIcon.setImageResource(bean.isCreater() ? R.drawable.ic_blue_channel_create : R.drawable.ic_blue_channel);
        setLockStatus(holder.channelLockIcon, bean.isLocked());
        setListenStatus(holder.listenIcon, bean);
    }

    @Override
    public int getItemCount() {
        return mGroupsData.size();
    }

    /**
     * 设置频道锁的状态UI
     *
     * @param view
     * @param isLock
     */
    private void setLockStatus(ImageView view, boolean isLock) {
        view.setImageResource(isLock ? R.drawable.ic_blue_channel_lock : R.drawable.ic_blue_channel_unlock);
    }

    /**
     * 设置频道监听状态UI
     *
     * @param view
     * @param bean
     */
    private void setListenStatus(ImageView view, GroupBean bean) {
        view.setVisibility(bean.isListen() ? View.VISIBLE : View.GONE);
    }

    class GroupsHolder extends RecyclerView.ViewHolder {
        /**
         * 群组Item的Icon
         */
        private ImageView channelIcon;
        /**
         * 正在监听频道的Icon
         */
        private ImageView listenIcon;
        /**
         * 群组名称
         */
        private TextView groupName;
        /**
         * 群组ID
         */
        private TextView groupId;
        /**
         * 有人正在对讲
         */
        private TextView talking;

        /**
         * 频道锁Icon
         */
        private ImageView channelLockIcon;
        /**
         * 频道锁点击事件
         */
        private View layoutLock;

        public GroupsHolder(View itemView) {
            super(itemView);
            channelIcon = (ImageView) itemView.findViewById(R.id.iv_channel);
            listenIcon = (ImageView) itemView.findViewById(R.id.iv_listen);

            groupName = (TextView) itemView.findViewById(R.id.tv_group_name);
            groupId = (TextView) itemView.findViewById(R.id.tv_group_id);
            talking = (TextView) itemView.findViewById(R.id.tv_talking);

            channelLockIcon = (ImageView) itemView.findViewById(R.id.iv_lock);
            layoutLock = itemView.findViewById(R.id.rl_lock);
        }
    }
}
